package com.busionline.wotu.woochenandroid;

import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.MenuItem;
import android.widget.FrameLayout;

import com.busionline.wotu.baselibrary.mvp.activity.BaseMvpActivity;
import com.busionline.wotu.baselibrary.navigation.bottom.TabBottomNavigation;
import com.busionline.wotu.baselibrary.navigation.bottom.iterator.ListTabIterator;
import com.busionline.wotu.woochenandroid.Fragment.HomeFragment;
import com.busionline.wotu.woochenandroid.Fragment.TreeFragment;
import com.busionline.wotu.woochenandroid.tools.FragmentManagerHelper;
import com.busionline.wotu.woochenandroid.tools.ResourceUtil;
import com.busionline.wotu.woochenandroid.views.TabMainItemView;

import butterknife.BindView;

/**
 * 主页
 *
 * @author woochen123
 * @time 2018/5/15 10:15
 * @desc
 */
public class MainActivity extends BaseMvpActivity {


    @BindView(R.id.container)
    FrameLayout container;
    @BindView(R.id.bottom_tab)
    TabBottomNavigation bottomTab;
    @BindView(R.id.navigation_view)
    NavigationView navigationView;
    @BindView(R.id.drawerLayout)
    DrawerLayout drawerLayout;
    private FragmentManagerHelper mFragmentManagerHelper;
    private HomeFragment mHomeFragment;
    private TreeFragment mTreeFragment;

    @Override
    protected boolean initToolBar() {
        mToolbar.setTitle(R.string.app_name);
        mToolbar.setNavigationIcon(R.drawable.ic_menu_white_24dp);
        return true;
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        initDrawable();
    }

    private void initDrawable() {
        ActionBarDrawerToggle mToggle = new ActionBarDrawerToggle(this, drawerLayout, mToolbar,
                R.string.navigation_drawer_open,
                R.string.navigation_drawer_close);
        mToggle.syncState();
        drawerLayout.addDrawerListener(mToggle);
        navigationView.setItemIconTintList(null);
        navigationView.setNavigationItemSelectedListener(onNavigationItemSelectedListener);
    }

    //设置侧滑item click
    private NavigationView.OnNavigationItemSelectedListener onNavigationItemSelectedListener = new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.menu_favorite_article:
             /*       if (!UserInfoManager.isLogin()) {
                        startActivity(new Intent(MainActivity.this, LogonActivity.class));
                    } else {
                        startActivity(new Intent(MainActivity.this, CollectArticleActivity.class));
                    }
                */
                break;
                case R.id.menu_about:
//                    startActivity(new Intent(MainActivity.this, AboutUsActivity.class));
                    break;
                case R.id.menu_exit:
//                    exitToLogin();
                    break;
            }
            return true;
        }
    };

    @Override
    protected void initData() {
        mFragmentManagerHelper = new FragmentManagerHelper(getSupportFragmentManager(), R.id.container);
        mHomeFragment = new HomeFragment();
        mFragmentManagerHelper.addFragment(mHomeFragment);
        initBottom();
    }

    private void initBottom() {
        TabMainItemView tabMainItemView = new TabMainItemView.Builder(mContext).setText(ResourceUtil.getString(R.string.home))
                .setImage(R.drawable.main_tab_selsctor)
                .build();
        TabMainItemView tabMainItemView1 = new TabMainItemView.Builder(mContext).setText(ResourceUtil.getString(R.string.system))
                .setImage(R.drawable.system_tab_selsctor)
                .build();
        ListTabIterator tabInterator = new ListTabIterator<TabMainItemView>();
        tabInterator.addItem(tabMainItemView);
        tabInterator.addItem(tabMainItemView1);
        bottomTab.addItem(tabInterator);
        bottomTab.setOnItemClickListener(new TabBottomNavigation.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                bottomTabClicks(position);
            }
        });
    }

    /**
     * 底部菜单切换
     * @param position
     */
    private void bottomTabClicks(int position) {
        switch (position) {
            case 0:
                mToolbar.setTitle(R.string.app_name);
                if(mHomeFragment == null){
                    mHomeFragment = new HomeFragment();
                }
                mFragmentManagerHelper.swithFragment(mHomeFragment);
                break;
            case 1:
                mToolbar.setTitle(R.string.system);
                if(mTreeFragment == null){
                     mTreeFragment = new TreeFragment();
                }
                mFragmentManagerHelper.swithFragment(mTreeFragment);
                break;
        }
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void showError() {

    }

    @Override
    public void showEmpty() {

    }
}
